package com.tvunetworks.center.user.config.datasource;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
 * @program:
 * @description:
 * @author HenryChen
 * @create: 6/11/2019 1:41 PM
 **/
@Aspect
@Component
@Slf4j
public class DataSourceAop {

	@Value("${readSuffix}")
	private String readSuffix;

	/**
	 *
	 * @param joinPoint joinPoint
	 */
	@Before("execution(* com.tvunetworks.center.user.service..*.*(..))")
	public void switchDataSourceType(JoinPoint joinPoint) {
		String[] readSuffixArr = readSuffix.split(",");
		for (String read : readSuffixArr) {
			String name = joinPoint.getSignature().getName().toLowerCase();
			if (name.startsWith(read)) {
				log.debug("dataSource switch to: Read");
				DataSourceContextHolder.read();
				return;
			}
		}
		log.debug("dataSource switch to: Write");
		DataSourceContextHolder.write();
	}

}